from django.db import models
import json


class DeviceData(models.Model):

    temperature = models.FloatField(max_length=1, blank=False)
    humidity = models.FloatField(max_length=1, blank=False)
    percentage = models.FloatField(max_length=1, blank=False)
    datetime = models.DateTimeField(auto_now_add=True, primary_key=True)

    def __str__(self):

        return str(self.datetime.ctime())

    @property
    def to_dict(self):
        ctime = {
            'year': self.datetime.year,
            'month': self.datetime.month,
            'day': self.datetime.day,
            'hour': self.datetime.hour,
            'minute': self.datetime.minute,
            'second': self.datetime.second,
            'microsecond': self.datetime.microsecond
        }
        return dict(temperature=self.temperature,
                    humidity=self.humidity,
                    percentage=self.percentage,
                    datetime=ctime)

    @property
    def to_dict2(self):
        ctime = '{year}.{month}.{day}.{hour}.{minute}.{second}.{microsecond}'.format(
            year=self.datetime.year,
            month=self.datetime.month,
            day=self.datetime.day,
            hour=self.datetime.hour,
            minute=self.datetime.minute,
            second=self.datetime.second,
            microsecond=self.datetime.microsecond
        )
        return dict(temperature=self.temperature,
                    humidity=self.humidity,
                    percentage=self.percentage,
                    datetime=ctime)

    @property
    def to_json(self):
        return json.dumps(self.to_dict)


class Predict(models.Model):

    predictValue = models.FloatField(max_length=1, blank=False)
    datetime = models.DateTimeField(auto_now_add=True, primary_key=True)

    def __str__(self):

        return str(self.datetime.ctime())

    @property
    def to_dict(self):
        ctime = {
            'year': self.datetime.year,
            'month': self.datetime.month,
            'day': self.datetime.day,
            'hour': self.datetime.hour,
            'minute': self.datetime.minute,
            'second': self.datetime.second,
            'microsecond': self.datetime.microsecond
        }
        return dict(predictValue=self.predictValue, datetime=ctime)

    @property
    def to_json(self):
        return json.dumps(self.to_dict)
